package baidu.hudson.plugin.result.collector.utils;

import hudson.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import hudson.model.*;
import hudson.tasks.*;
import java.util.regex.*;
import javax.xml.parsers.*;
import java.net.URLEncoder;
import org.kohsuke.stapler.*;
import net.sf.json.JSONObject;
import hudson.util.FormValidation;
import javax.servlet.ServletException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

public class HttpRequest
{
    private String domain;
    private int port;
    
    public HttpRequest(String domain, int port) {
        this.domain = domain;
        this.port = port;
    }
    
    public void doPost(String uri, NameValuePair[] nameValuePair)
    {
        try
        {
            HttpClient client = new HttpClient();
            client.getHostConfiguration().setHost(domain, port, "http");
            PostMethod method = new PostMethod(uri);
            
            method.setRequestBody(nameValuePair);
            client.executeMethod(method);
            System.out.println(method.getStatusLine());
            InputStream istream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(istream));
            String buffer = "";
            while ((buffer = br.readLine()) != null) {
                System.out.println(buffer);
            }
            method.releaseConnection();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void doGet(String uri) {
        try
        {
            HttpClient client = new HttpClient();
            GetMethod method = new GetMethod("http://"+domain+":"+port+uri);
            
            client.executeMethod(method);
            System.out.println(method.getStatusLine());
            InputStream istream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(istream));
            String buffer = "";
            while ((buffer = br.readLine()) != null) {
                System.out.println(buffer);
            }
            method.releaseConnection();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

